var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
// 实例化API核心类
var qqmapsdk = new QQMapWX({
    key: 'SNOBZ-UXT6V-SYTPO-UZZXI-5LZAH-M2FDL' // 必填 /*换域名 */
});
const app = getApp(),url = app.globalData.url;
var WxParse = require('../wxParse/wxParse.js');
Page({
  data: {
    sx:1,
    zwwhere:{
      enterprise_uid:'',
      pageNum:1,
      pageSize:300,
    },
    zznum:0,
    isguan:1,
    listdata:[],//zhiweiliebiao 

    readmore: {
      status: true,
      tip: '查看更多',rrow:'https://res.tuoluohuodong.com/green_bottom_rrow.png'    
    }
  },
  toggle() {
    const status = this.data.readmore.status
    
    this.setData({
    readmore: {
    status: !status,
    
    tip: status ? '收起' : '查看更多',
    
    rrow: status ? 'https://res.tuoluohuodong.com/green_top_rrow.png' : 'https://res.tuoluohuodong.com/green_bottom_rrow.png'
    
    }
    
    })
    
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this
    var id = options.id
    that.setData({
      guid:options.guid,//关注id
      enterprise_uid:options.id
    })
    wx.showLoading({title: '加载中',})
    wx.request({
      url:url+'enterprise/getEnterpriseInfoByUid',//getEnterpriseInfo
      method: "GET",
      data:{enterprise_uid:options.id},//enterprise_uid
      success:function(res){
        console.log("详情 ",res);
        var ret = res.data
        if(ret.code == 200){
          that.zwlist(ret.data.enterpriseUid)//查在赵薇列表
          wx.hideLoading()
          // var s = "j五险一金,j福利待遇,j定期体检"
          if(ret.data.treatment != null){
            var s = ret.data.treatment//welfareRemark
            var ss = s.split(",");
            that.setData({
              ss:ss
            })
          }
          that.setData({
            detail:ret.data,
            uid:ret.data.enterpriseUid
          })
          var content = ret.data.introduction//enterpriseInfo 企业信息   introduction 企业简介
          WxParse.wxParse('article', 'html',content, that, 5);
        }
      }
    })

    // 关注列表/dgdx-Api/interest/getInterestList
    var user_massage = wx.getStorageSync('user_massage')
    wx.request({
      url:url+'interest/getInterestList',
      method: "GET",
      data:{
        user_uid:user_massage.userUid,
        pageNum :1,
        pageSize :999,
      },
      success:function(res){
        console.log("关注列表 ",res);
        var ret = res.data///isguan
        if(ret.code == 200){
          for(let i of ret.data.content){
            if(i.enterpriseUid == id){
              that.setData({isguan:2})
            }
            break;
          }
        }
      }
    })
  },
  detail(e){
    let did = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/HomePage/workdetail/workdetail?id='+did,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  // 切换
  butt(e){
    var that =this
    var sx = e.currentTarget.dataset.tab
    console.log(sx)
    that.setData({sx})
    // if(sx == 2){
      var id = that.data.uid
      that.zwlist(id)
    // }
  },
  zwlist(id){
    // console.log(id)
    var that = this
    that.data.zwwhere.enterprise_uid = id
    var zwwhere = that.data.zwwhere
    wx.request({
      url:url+'enterprise/getPositionByEnterpriseId',
      method: "GET",
      data:zwwhere,
      success:function(res){
        console.log("职位列表 ",res);
        var ret = res.data
        if(ret.code == 200){
          wx.hideLoading()
          // listdata=listdata.concat(arr2)
          that.setData({
            listdata:ret.data.content,
            zznum:ret.data.content.length
          })
        }
      }
    })
  },
  // 搜索
  searchBut:function(e){
    var that = this
    let status = e.detail.value
    var enterprise_uid = that.data.enterprise_uid
    console.log('执行search',status)
    wx.request({
      header: {
        'content-type': 'application/json' // 默认值
      },
      url:url+ 'detail/getAllEnterpriseDetailPositionByPage',
      method: "GET",
      data:{
        enterprise_uid :enterprise_uid,
        pageNum:1,
        pageSize:150,
        position_name:status,
      },
      success:function(res){
        console.log("搜索",res);
        var ret = res.data
        if(ret.code == 200){
          var listdata = ret.data.content
          var num = ret.data.content.length
          that.setData({listdata:listdata,zznum:num})
        }else{
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
        }
      }
    })
  },
  daohang(e){
    var that = this
    var store = e.currentTarget.dataset.store
    var address = e.currentTarget.dataset.address
    console.log(store)
    console.log(address)
    //根据地点 逆查询地点的经纬度
    qqmapsdk.geocoder({
      address: address,
      complete: res => {
          console.log(res);   //经纬度对象
          if(res.status == 0){
            var latlon = res.result.location
            console.log('latlon',latlon)
            // wx.getLocation({
            //   type: 'wgs84',
            //   success: function (res) {
            //     console.log(res)
                wx.openLocation({//​使用微信内置地图查看位置。
                  latitude:  Number(latlon.lat,),//要去的纬度-地址
                  longitude: Number(latlon.lng),//要去的经度-地址
                  name: store,
                  address: address
                })
            //   },
            //   fail(res){
            //     if(res.errCode == 2){
            //         wx.showToast({
            //           title: '定开启定位...',
            //           icon:'none',
            //           duration:1500,
            //         })
            //     }
            //   },
            // })

          }else{
            wx.showToast({
              title: '数据信息有误~',
              icon:'none',
              duration:1500,
            })
          }
      }
    })

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  // 关注
  addguanzhu(e){
    var that = this
    wx.showLoading({
      title: '加载中...',
    })
    var sts = e.currentTarget.dataset.sts
    var enterprise_uid = e.currentTarget.dataset.id
    var user_massage = wx.getStorageSync('user_massage')
    wx.request({
      url:url+'interest/addToInterest',
      method: "POST",
      data:{
        enterpriseUid:enterprise_uid,
        userUid: user_massage.userUid
      },
      success:function(res){
        wx.hideLoading()
        console.log("关注",res);
        var ret = res.data
        if(ret.code == 200){
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
          that.setData({
            isguan:sts,
            guid:ret.data.id,//关注id
          })
        }else{
          wx.hideLoading()
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
        }
      }
    })
  },
  quxiaoguanzhu(e){
    var that = this
    var sts = e.currentTarget.dataset.sts
    var guid = that.data.guid//收藏的ID
    wx.showLoading({
      title: '加载中...',
    })
    console.log(guid)
    if(guid == undefined){
      wx.showToast({
        title: '请前往收藏列表~',
        icon:'none',
      })
      wx.hideLoading()
      return false
    }
    wx.request({
      url:url+'interest/cancelInterest?interest_id='+guid,
      method: "DELETE",
      data:{
      },
      success:function(res){
        console.log("",res);
        wx.hideLoading()
        var ret = res.data
        if(ret.code == 200){
          wx.showToast({
            title: '取消关注成功',
            icon:'none'
          })
          that.setData({
            isguan:sts
          })
        }else{
          wx.hideLoading()
          wx.showToast({
            title: '取消关注失败',
            icon:'none'
          })
        }
      }
    })
  },
})